import java.util.*;

public class Market {

    //水果商品
    private Map<String, Fruit> fruits = new HashMap<>();
    //水果折扣活动
    private Map<String, Activity> fruitsDiscountActivity = new HashMap<>();
    //满减活动
    private List<Activity> fullReductionActivity = new ArrayList<>();

    /**
     * 水果上架/更新价格
     */
    public void addFruit(Fruit fruit) {
        fruits.put(fruit.getName(), fruit);
    }

    /**
     * 水果下架
     */
    public void removeFruit(String fruitName) {
        fruits.remove(fruitName);
    }

    /**
     * 针对顾客结算价格
     */
    public double settle(Customer customer) {
        Map<String, Integer> fruitCountMap = customer.getFruitCountMap();
        double totalPrice = 0.0;
        for (Map.Entry<String, Integer> entry : fruitCountMap.entrySet()) {
            double discount = 1;
            //拿到水果的折扣活动
            Activity activity = fruitsDiscountActivity.get(entry.getKey());
            if (activity != null) {//如果这个水果有折扣活动，拿取折扣
                discount = activity.getDiscount();
            }
            totalPrice += fruits.get(entry.getKey()).getPrice() * discount * entry.getValue();
        }
        //满减活动
        for (Activity activity : fullReductionActivity) {//满减活动扣减从最大的开始，
            if (totalPrice >= activity.getTotalMoney()) {
                totalPrice -= activity.getSubtractMoney();
                break;
            }
        }
        return totalPrice;
    }

    //添加活动
    public void addActivity(Activity activity) {
        if (activity.getType() == 1) {
            fruitsDiscountActivity.put(activity.getFruitName(), activity);
        } else {
            fullReductionActivity.add(activity);
            fullReductionActivity.sort(Comparator.comparing(Activity::getSubtractMoney).reversed());
            //满减金额排序从大到小，优先扣除满减金额大的
        }
    }

    public void removeActivity(Activity activity) {
        //TODO
    }
}
